//
//  TTCategoryViewController.m
//  TaoTuo
//
//  Created by admin on 2019/8/20.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTCategoryViewController.h"
#import "LLSegmentTableView.h"
#import "TTCategoryModel.h"
#import "TTGoodsListViewController.h"

@interface TTAdditionalModel : NSObject

@property (nonatomic, copy) NSString *imageName;
@property (nonatomic, copy) NSString *pinyin;
@property (nonatomic, strong) UIColor *bgColor;

+ (instancetype)setupWithImageName:(NSString *)imageName pinyin:(NSString *)pinyin bgColor:(UIColor *)bgColor;

@end

@implementation TTAdditionalModel

+ (instancetype)setupWithImageName:(NSString *)imageName pinyin:(NSString *)pinyin bgColor:(UIColor *)bgColor
{
    TTAdditionalModel *model = [[self alloc] init];
    model.imageName = imageName;
    model.pinyin = pinyin;
    model.bgColor = bgColor;
    return model;
}

@end

@interface TTCategoryViewController ()<LLSegmentTableViewDelegate>

@property (nonatomic, strong) LLSegmentTableView *segTableView;
@property (nonatomic, strong) NSMutableArray *titleArray;
@property (nonatomic, strong) NSMutableArray *datasArray;
@property (nonatomic, strong) NSDictionary *categoryDic;

@end

@implementation TTCategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"分类";
    
    
    // 数组顺序：【图片名，拼音，】
    self.categoryDic = @{
                         @"女戒" : [TTAdditionalModel setupWithImageName:@"category_nvjie" pinyin:@"Women's ring" bgColor:UIColorHex(#F4E3FF)],
                         @"男戒" : [TTAdditionalModel setupWithImageName:@"category_nanjie" pinyin:@"Men's ring" bgColor:UIColorHex(#DCE3FF)],
                         @"对戒" : [TTAdditionalModel setupWithImageName:@"category_duijie" pinyin:@"Lovers' ring" bgColor:UIColorHex(#FFE5FF)],
                         @"吊坠" : [TTAdditionalModel setupWithImageName:@"category_diaozhui" pinyin:@"Pendant" bgColor:UIColorHex(#ECFFE4)],
//                         @"项链" : [TTAdditionalModel setupWithImageName:@"category_xianglian" pinyin:@"Necklace" bgColor:UIColorHex(#D8FFF8)],
                         @"套链" : [TTAdditionalModel setupWithImageName:@"category_taolian" pinyin:@"Chain set" bgColor:UIColorHex(#D6F2FF)],
                         @"手链" : [TTAdditionalModel setupWithImageName:@"category_shoulian" pinyin:@"Bracelet" bgColor:UIColorHex(#FFD6FA)],
                         @"手镯" : [TTAdditionalModel setupWithImageName:@"category_shouzhuo" pinyin:@"bracelet" bgColor:UIColorHex(#DCE3FF)],
                         @"耳坠" : [TTAdditionalModel setupWithImageName:@"category_erzhui" pinyin:@"Ear pendant" bgColor:UIColorHex(#FFEBD1)],
                         @"耳钉" : [TTAdditionalModel setupWithImageName:@"category_erding" pinyin:@"Earnail" bgColor:UIColorHex(#D6FAFF)],
                         @"耳线" : [TTAdditionalModel setupWithImageName:@"category_erxian" pinyin:@"Ear wires" bgColor:UIColorHex(#F5FFE4)],
                         @"耳环" : [TTAdditionalModel setupWithImageName:@"category_erhuan" pinyin:@"earring" bgColor:UIColorHex(#D8FFF8)]
                         };
    
    
    [self getNetworkData];
    
    
}

- (void)view:(LLSegmentTableView *)lgView didSelectRowAtIndexPath:(NSIndexPath *)indexPath model:(TTCategorySubModel * _Nullable)model
{
//    NSLog(@"%ld, %ld, title = %@", indexPath.section, indexPath.row, model.name);
    UIViewController *vc = [[CTMediator sharedInstance] goodsList_viewControllerWithTitle:model.name index:0];
    [self.navigationController pushViewController:vc animated:YES];
}


// 获取数据
- (void)getNetworkData
{
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] requestMethod:GET urlString:categoryList_api parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        if (self.segTableView == nil) {
            self.segTableView = [[LLSegmentTableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
            self.segTableView.delegate = self;
            [self.view addSubview:self.segTableView];
        }
        
//        for (NSDictionary *dic in responseObject) {
//            TTCategoryModel *model = [TTCategoryModel modelWithJSON:dic];
//            [self.titleArray addObject:model.categoryName];
//            [self.datasArray addObject:model.categoryData];
//        }
//
//        for (NSArray *arr in self.datasArray) {
//            for (TTCategorySubModel *model in arr) {
//                TTAdditionalModel *adModel = self.categoryDic[model.name];
//                if (adModel) {
//                    model.imageName = adModel.imageName;
//                    model.pinyin = adModel.pinyin;
//                    model.bgColor = adModel.bgColor;
//                }
//            }
//        }
//        [self.segTableView setTitleArray:self.titleArray detailArray:self.datasArray];
        
        NSMutableArray *array = [NSMutableArray array];
        for (NSDictionary *dic in responseObject) {
            TTCategoryModel *model = [TTCategoryModel modelWithJSON:dic];
            [self.titleArray addObject:model.categoryName];
            [array addObject:model.categoryData];
        }
        
        NSMutableArray *datas = [NSMutableArray array];
        for (NSArray *arr in array) {
            for (TTCategorySubModel *model in arr) {
                TTAdditionalModel *adModel = self.categoryDic[model.name];
                if (adModel) {
                    model.imageName = adModel.imageName;
                    model.pinyin = adModel.pinyin;
                    model.bgColor = adModel.bgColor;
                    [datas addObject:model];
                }
            }
        }
        [self.datasArray addObject:datas];
        
        [self.segTableView setTitleArray:@[@"钻饰"] detailArray:self.datasArray];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    } showLoadingInView:self.view];
}


- (NSMutableArray *)titleArray
{
    if (!_titleArray) {
        _titleArray = [NSMutableArray array];
    }
    return _titleArray;
}

- (NSMutableArray *)datasArray
{
    if (!_datasArray) {
        _datasArray = [NSMutableArray array];
    }
    return _datasArray;
}

@end
